import { ref } from 'vue'
import { findTopCategory } from '@/api/category'
import { useRoute, onBeforeRouteUpdate } from 'vue-router'
export function useCategory () {
  // 获取分类页面列表功能
  const categroyAllList = ref([])
  const route = useRoute()
  async function getCategroyAllList (id = route.params.id) { // 默认参数，当传入参数时使用传入的参数，否则使用默认参数
    const res = await findTopCategory(id)
    categroyAllList.value = res.data.result
  }
  getCategroyAllList()

  // 路由更新钩子函数
  onBeforeRouteUpdate((to) => {
    // console.log(to)//是指要去的路由对象
    getCategroyAllList(to.params.id)
  })

  return { categroyAllList }
}
